/**
 * @desc:平安银行“平安结算通”商户服务协议
 */

import { useState, useEffect, useRef } from 'react';
import {
  Checkbox,
  Alert,
  message,
} from 'antd';
import { getCurrentUser } from '@/utils/authority';
import CommonModal from '@/components/CommonModal';
import CommonLoading from '@/components/CommonLoading';
import { getLastLoginIp, registerBehaviorOp } from '../service';

export default ({ accountNo, handleOk, handleCancel }) => {

  const [loading, setLoading] = useState(false);

  const [ipAddress, setIpAddress] = useState('');

  const stageData = useRef({
    checkResult: undefined,
    businessNo: undefined,
    otpOrderNo: undefined,
    checkBoxOne: false,
    checkBoxTwo: false,
  });

  useEffect(() => {
    getIpAddress();
  }, []);

  // 获取登录ip
  const getIpAddress = async () => {
    setLoading(true);
    const { data, success } = await getLastLoginIp({
      tenantCode: '000000',
      uuid: getCurrentUser().uuid,
    });
    if (success) {
      setIpAddress(data?.lastLoginIp);
    }
    setLoading(false);
  };

  //提交签约
  const onOk = async () => {
    const { checkBoxOne } = stageData.current;
    if (!checkBoxOne) {
      return message.info('请勾选和同意用户条款');
    }
    setLoading(true);
    const { success } = await registerBehaviorOp({
      accountNo,
      ipAddress,
      // macAddress: '00:00:00:00:00',
      signChannel: 2
    });
    if(success){
      message.success('签约成功！')
      handleOk();
    }
  };

  return (
    <CommonModal
      title="温馨提示"
      visible={true}
      width="488px"
      onCancel={handleCancel}
      onOk={onOk}
    >
      <Alert message="请您仔细阅读以下协议，充分了解后勾选同意授权:" />
      <p style={{margin:"8px 4px", textIndent:'26px'}}>为有助于您更加了解平安银行“平安结算通”业务为您提供的服务内容及在您使用该服务的权利和义务，请您确认已仔细阅读并充分理解《平安银行“平安结算通”商户服务协议》的全部条款，特别是其中所涉及的免除甲方责任和限制乙方权利的条款，请特别关注加粗内容。</p>
      <div>
        <Checkbox
          onChange={(e) => (stageData.current.checkBoxOne = e.target.checked)}
        />
        &nbsp;&nbsp;我已阅读并同意
        <a type="link" href="https://my.orangebank.com.cn/orgLogin/hd/act/jianzb/jzbxy.html" target="blank">
          《平安银行“平安结算通”商户服务协议》
        </a>
      </div>
      
      <CommonLoading loading={loading} />
    </CommonModal>
  );
};
